#include "EventLoop.h"

EventLoop::EventLoop()
    : epfd_(new Epoll)
{
}

EventLoop::~EventLoop()
{
    delete epfd_;
}

std::vector<Channel *> EventLoop::run()
{
    while (true)
    {
        std::vector<Channel *> channels = epfd_->loop(10*1000);
        if (channels.empty())
        {
            if (timeCallBack_)
                timeCallBack_(this);
        }
        else
        {
            for (auto &ch : channels)
            {
                ch->handleevent();
            }
        }
    }
}

Epoll *EventLoop::getEpfd()
{
    return epfd_;
}

void EventLoop::updateChannel(Channel *ch)
{
    epfd_->updateChannel(ch);
}

void EventLoop::setTimeCallBack(func_t fn)
{
    timeCallBack_ = fn;
}